package com.tz.farmer.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tz.farmer.model.ShareRecord;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface ShareRecordMapper extends BaseMapper<ShareRecord> {

    /**
     * kai 总条数
     * @return
     */
    @Select("<script>" +
            "select   \n" +
            "count(1) \n" +
            "from check_apply \n" +
            "where property = #{flag} and 1 = 1  " +
            " <if test = \"keyword != null and keyword.trim() !=''  \" > and (p_name like CONCAT('%', #{keyword},'%')  \n" +
            " or m_name like CONCAT('%', #{keyword},'%')  \n" +
            " or classify like CONCAT('%', #{keyword},'%') ) </if> \n" +
            " <if test=\"startTime != null and startTime.trim() != ''\">" +
            "   <![CDATA[   and ( STR_TO_DATE(share_time, '%Y-%m-%d %H:%i:%s') >=  STR_TO_DATE( '${startTime} 00:00:00', '%Y-%m-%d %H:%i:%s')   \n" +
            "     and STR_TO_DATE(share_time, '%Y-%m-%d %H:%i:%s') <= STR_TO_DATE( '${endTime} 23:59:59', '%Y-%m-%d %H:%i:%s')  )  ]]> \n" +
            " </if> " +
            "</script>")
    int countFlag( @Param("flag")Integer flag,@Param("keyword") String keyword,@Param("startTime") String startTime, @Param("endTime")String endTime);

    /**
     * kai 条件查询
     * @return
     */
    @Select("<script>" +
            "select   \n" +
            " id, \n" +
            " classify, \n" +
            " p_name, \n" +
            " m_name, \n" +
            " user_info, \n" +
            " channel, \n" +
            " share_time, \n" +
            " property\n" +
            "FROM\n" +
            "\tshare_record" +
            " where property = #{flag} and 1 = 1  " +
            " <if test = \"keyword != null and keyword.trim() !=''  \" > and ( p_name like CONCAT('%', #{keyword},'%')  \n" +
            " or m_name like CONCAT('%', #{keyword},'%')  \n" +
            " or classify like CONCAT('%', #{keyword},'%') ) </if> \n" +
            " <if test=\"startTime != null and startTime.trim() != ''\">" +
            "   <![CDATA[   and ( STR_TO_DATE(share_time, '%Y-%m-%d %H:%i:%s') >=  STR_TO_DATE( '${startTime} 00:00:00', '%Y-%m-%d %H:%i:%s')   \n" +
            "     and STR_TO_DATE(share_time, '%Y-%m-%d %H:%i:%s') <= STR_TO_DATE( '${endTime} 23:59:59', '%Y-%m-%d %H:%i:%s')  )  ]]> \n" +
            " </if> " +
            "  order by share_time DESC limit #{n} , #{m} " +
            "</script>")
    List<ShareRecord> selectConditionList(@Param("flag")Integer flag,@Param("keyword") String keyword,@Param("startTime") String startTime, @Param("endTime")String endTime,@Param("n") int n,@Param("m") int m);
    /**
     * kai 条件查询
     * @return
     */
    @Select("<script>" +
            "select   \n" +
            " id, \n" +
            " classify, \n" +
            " p_name, \n" +
            " m_name, \n" +
            " user_info, \n" +
            " channel, \n" +
            " share_time, \n" +
            " property\n" +
            " FROM\n" +
            " share_record" +
            " where property = #{flag} and 1 = 1  " +
            " <if test = \"keyword != null and keyword.trim() !=''  \"> and ( p_name like CONCAT('%', #{keyword},'%')  \n" +
            " or m_name like CONCAT('%', #{keyword},'%')  \n" +
            " or classify like CONCAT('%', #{keyword},'%') ) </if> \n" +
            " <if test=\"startTime != null and startTime.trim() != ''\">" +
            "   <![CDATA[   and ( STR_TO_DATE(share_time, '%Y-%m-%d %H:%i:%s') >=  STR_TO_DATE( '${startTime} 00:00:00', '%Y-%m-%d %H:%i:%s')    \n" +
            "      and STR_TO_DATE(share_time, '%Y-%m-%d %H:%i:%s') <= STR_TO_DATE( '${endTime} 23:59:59', '%Y-%m-%d %H:%i:%s')  )  ]]> \n" +
            " </if> " +
            " <if test = 'ids != null and ids.length != 0 ' > and id in  \n" +
            "   <foreach collection='ids' item='ids' open=\"(\" separator=\",\" close=\")\">\n" +
            "        #{ids}\n" +
            "        </foreach>"+
            "         </if>"+
            "  order by share_time DESC  " +
            "</script>")
    List<ShareRecord> selectDownLoadList(@Param("flag")Integer flag,@Param("keyword") String keyword,@Param("startTime") String startTime, @Param("endTime")String endTime,@Param("ids") Integer[] ids);

}
